<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-15
 * Time: 15:21
 * 维保计划  表
 */

namespace app\index\controller;


class Maintenance
{
    const maintenance = [
        'deviceId'=>'',        //设备id
        'deviceName'=>'',
        'cycle'=>[],          //维保日期
    ];

    public static function getData($where, $one = true, $table = 'maintenance')
    {
        return ($one == true) ? getFirst(mongo($table)->where($where)->select()) : mongo($table)->where($where)->select();
    }
    public static function setData($where, $data, $table = 'maintenance')
    {
        mongo($table)->where($where)->update($data);
    }
    public static function insertData($data, $table = 'maintenance')
    {
        mongo($table)->insert($data);
        return mongo($table)->getLastInsID();
    }
    public static function deleteData($where, $table = 'maintenance')
    {
        mongo($table)->where($where)->delete();
    }

    //获取所有专业维保计划书和设备ID
    public static function getMaintenanceMenuDb()
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $dataList = [];   //数据
        $data = EquipRecord::getData(['parentId'=>User::getStoreId()], false); //获取所有专业
        foreach ($data as $major)
        {
            array_push($dataList, ['majorId'=>$major['id'], 'majorMenu'=>$major['name'], 'planMenu'=>$major['name'].'维保计划书']);
        }
        return RD(SUCCESS, $dataList);
    }

    /**
     * 根据专业获取所有设备和检修周期
     * @param string $majorId  专业id
     * @return array
     */
    public static function getDeviceCycleDb($majorId = null)
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;
        $majorId        = empty($majorId) ? input('majorId') : $majorId;
        $allDeviceCycle = [];
        $data           = [];

        EquipRecord::getDevicesByType($majorId, '设备', $data);  //获取所有设备

        foreach ($data as $device) {

            $deviceCycle = self::getData(['deviceId'=>$device['id']]);
            if(empty($deviceCycle)) {
                self::insertData(array_merge(self::maintenance, ['deviceId'=>$device['id'], 'deviceName'=>$device['name']]));  //自动动态添加
            } else if($device['name'] != $deviceCycle['deviceName']){
                self::setData(['deviceId'=>$device['id']], ['deviceName'=>$device['name']]);
            }
            $deviceCycle = self::getData(['deviceId'=>$device['id']]);
            array_push($allDeviceCycle, $deviceCycle);
        }
        return RD(SUCCESS, $allDeviceCycle);
    }

    /**
     * 编辑设备维保计划
     * @param string $deviceId    设备id
     * @param string $date        日期  1--3--6  月--第几个星期--月检/年检
     * @return array
     */
    public static function editDeviceMaintenanceDb($deviceId = null, $date =null )
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $deviceId = input('deviceId');
        $date     = input('date');
        $getArr   = explode("--", $date);

        if(empty($deviceId) || count($getArr) != 3)
            return RD(PARA_NOT_NULL);

        $cycles = self::getData(['deviceId'=>$deviceId])['cycle'] ?? [];  //获取设备检修周期
        foreach ($cycles as $key=>$cycle) {
            $arr = explode("--", $cycle);

            if(count($arr) >=3 && $getArr[0] == $arr[0] && $getArr[1] == $arr[1]) {
                if($getArr[2] != $arr[2]) {
                    $cycles[$key] = $date;
                    self::setData(['deviceId'=>$deviceId], ['cycle.'.$key=>$date]);
                }
                return RD(SUCCESS, $date);
            }
        }
        self::setData(['deviceId'=>$deviceId], ['cycle.'.count($cycles)=>$date]);
        return RD(SUCCESS, $date);
    }
    //手机获取维保计划
    public static function  appGetDeviceCycle()
    {
        if(!empty($perssion = perssion([LOGIN, SHOP])))   //权限判断
            return $perssion;

        $devicesCycles = self::getDeviceCycleDb()['data'];
        foreach ($devicesCycles as $key=>$deviceCycle) {
            $cycles = DArray($deviceCycle, 'cycle');
            $emptyArray = ['', '', '', ''];
            $plans = [$emptyArray, $emptyArray, $emptyArray, $emptyArray, $emptyArray, $emptyArray, $emptyArray, $emptyArray, $emptyArray, $emptyArray, $emptyArray, $emptyArray];
            foreach ($cycles as $cycle) {
                $cycleArray = explode("--", $cycle);
                $plans[($cycleArray[0]??'1') - 1][($cycleArray[1]??'1') - 1] = $cycleArray[2] ?? '1';
            }
            $devicesCycles[$key]['plans'] = $plans;
        }
        return RD(SUCCESS, $devicesCycles);
    }
}
